﻿using System;
using System.Collections.Generic;
using System.Text;

using Gx.Model.Role;//实体
using Gx.Dal.Role;
using Gx.Common;
using Gx.Model;
using Gx.Model.Enums;
using Gx.Model.Other;

namespace Gx.Bll.Role
{
    /// <summary>
    /// 
    /// </summary>
    public class M_RoleInfoBll
    {
        /// <summary>
        /// 加载所有角色
        /// </summary>
        /// <param name="List"></param>
        /// <returns></returns>
        public List<TreeView> GetRoleJson()
        {
            List<TreeView> tvList = new List<TreeView>();
            List<M_RoleInfo> roleList = GetRoleInfoList();
            foreach (var item in roleList)
            {
                tvList.Add(new TreeView()
                {
                    id = item.RoleId,
                    text = item.RoleName
                });
            }
            return tvList;
        }


        public ReturnModel AddRoleInfo(M_RoleInfo model)
        {
            using (M_RoleInfoDal dal = new M_RoleInfoDal())
            {
                ReturnModel r = new ReturnModel()
                {
                    Code = (int)Code.操作失败,
                    Msg = "操作失败",
                    Success = false,
                };
                if (model.RoleName.IsNullOrEmpty())
                {
                    r.Msg = "角色名称不能为空!";
                    return r;
                }
                bool res = dal.AddRoleInfo(model);
                if (res)
                {
                    r.Code = (int)Code.操作成功;
                    r.Msg = "操作成功";
                    r.Success = true;
                }
                return r;
            }
        }

        public ReturnModel DelRoleInfo(int RoleId)
        {
            using (M_RoleInfoDal dal = new M_RoleInfoDal())
            {
                ReturnModel r = new ReturnModel()
                {
                    Code = (int)Code.操作失败,
                    Msg = "操作失败",
                    Success = false,
                };
                if (RoleId == (int)Roles.管理员)
                {
                    r.Msg = "系统默认角色,无法删除";
                    return r;
                }
                bool res = dal.DelRoleInfo(RoleId);
                if (res)
                {
                    r.Code = (int)Code.操作成功;
                    r.Msg = "操作成功";
                    r.Success = true;
                }
                return r;
            }
        }

        public ReturnModel EditRoleInfo(M_RoleInfo model)
        {
            using (M_RoleInfoDal dal = new M_RoleInfoDal())
            {
                ReturnModel r = new ReturnModel()
                {
                    Code = (int)Code.操作失败,
                    Msg = "操作失败",
                    Success = false,
                };
                if (model.RoleName.IsNullOrEmpty())
                {
                    r.Msg = "角色名称不能为空!";
                    return r;
                }
                bool res = dal.EditRoleInfo(model);
                if (res)
                {
                    r.Code = (int)Code.操作成功;
                    r.Msg = "操作成功";
                    r.Success = true;
                }
                return r;
            }
        }

        public List<M_RoleInfo> GetRoleInfoList(string roleName, int pageIndex, int pageSize, out int totalCount)
        {
            using (M_RoleInfoDal dal = new M_RoleInfoDal())
            {
                int PageIndex = (pageIndex - 1) * pageSize;
                if (PageIndex > 0) PageIndex += 1;
                int PageSize = pageIndex * pageSize;
                StringBuilder strWhere = new StringBuilder();
                if (!roleName.IsNullOrEmpty())
                {
                    strWhere.AppendFormat(" AND RoleName LIKE'%{0}%' ", roleName);
                }
                return dal.GetRoleInfoList(strWhere.ToString(), PageIndex, PageSize, out totalCount);
            }
        }

        public List<M_RoleInfo> GetRoleInfoList()
        {
            using (M_RoleInfoDal dal = new M_RoleInfoDal())
            {
                return dal.GetRoleInfoList();
            }
        }


        public M_RoleInfo GetRoleInfoModel(int id)
        {
            using (M_RoleInfoDal dal = new M_RoleInfoDal())
            {
                return dal.GetM_RoleInfoModel(id);
            }
        }
    }
}
